package com.bdqn.crm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.crm.mapper.SysUserMapper;
import com.bdqn.crm.pojo.PageBean;
import com.bdqn.crm.pojo.Pagee;
import com.bdqn.crm.pojo.SysUser;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser getlogin(String usrName) {
//        QueryWrapper<SysUser> qw = new QueryWrapper<>();
//        qw.eq("usr_name",usrName);
        return sysUserMapper.getlogin(usrName);
    }

//    @Override
//    public List<SysUser> getPageList(SysUser sysUser, Pagee pagee) {
//        int pageIndex=(pagee.getCurrPageNo()-1) * pagee.getPageSize();
//        return sysUserMapper.getPageList(sysUser,pageIndex,pagee.getPageSize());
//    }

    @Override
    public PageBean<SysUser> pages(String usrName, Long usrRoleId, Integer pageIndex) {
        System.out.println(usrRoleId+"aa");
        PageBean<SysUser> pageBean=new PageBean<>();

        //数据库第几行开始查询
        int startPage=(pageIndex-1)*pageBean.getPageCount();
        //查询多少行数据 分页类里默认5行
        int selectCount=pageBean.getPageCount();
        //获取mapper层的数据
        List<SysUser> list=sysUserMapper.lists(usrName,usrRoleId,startPage,selectCount);

        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        Map map=new HashMap();
        map.put("usr_name",usrName);
        map.put("usr_role_id",usrRoleId);
        qw.allEq(map,false);
        //获取总数
        int cityCount=sysUserMapper.selectCount(qw);
        pageBean.setCurrent(pageIndex);
        //设置查询的数据
        pageBean.setRecords(list);
        //共多少行
        pageBean.setTotal(cityCount);
        //设置总页数
        pageBean.setTotalPage(cityCount);
        System.out.println("总记录数 ---- --> " + pageBean.getTotal());
        System.out.println("总页数 ------ --> " + pageBean.getPages());
        System.out.println("当前页数 ----- -> " + pageBean.getCurrent());
        System.out.println("每页记录数 -----> " + pageBean.getPageCount());
        System.out.println("当前记录： -----> ");
        return pageBean;
    }

//    @Override
//    public int getPageListTotal(SysUser sysUser) {
//        return sysUserMapper.getPageListTotal(sysUser);
//    }
    /**
     * 根据id拿到数据
     * @param usrId
     * @return
     */
    @Override
    public SysUser getUser(Long usrId) {
        return sysUserMapper.getUser(usrId);
    }

    @Override
    public boolean deleSysUser(Long usrId) {
        return sysUserMapper.deleSysUser(usrId);
    }

    @Override
    @Cacheable(value = "user",keyGenerator = "keyGenerator")
    public SysUser getSysUser(Long usrId) {
        return sysUserMapper.selectById(usrId);
    }

    /**
     * 根据用户名查询用户对象
//     * @param usrName
     * @return
     */
//    @Override
//    public SysUser getUsrName(String usrName) {
//        QueryWrapper<SysUser> qw = new QueryWrapper<>();
//        qw.eq("usr_name",usrName);
//        return sysUserMapper.selectOne(qw);
//    }
    /**
     * 密码加密
     * @param encryptPassword
     * @return
     */
    public String  encryptPassword(Object  encryptPassword) throws IllegalArgumentException{
        String salt = "czkt";
        Md5Hash md5Hash = new Md5Hash(encryptPassword,salt,2);
        return md5Hash.toString();
    }
}
